package com.longrise.filedownloaderdemo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.longrise.filedownloader.DownloadManager;
import com.longrise.filedownloader.DownloadManager.DownloadStatus;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 16/4/15.
 *
 */
public class DownloadedDetailAdapter extends BaseAdapter{


    private final Context mContext;
    private final DownloadManager mDownloadManager;
    private List<String> mUrlList = new ArrayList<>();
    private UpdateCallBack updateCallback;


    public DownloadedDetailAdapter(Context context, DownloadManager downloadManager){
        this.mContext = context;
        this.mDownloadManager = downloadManager;
    }

    public void setUpdateCallback(UpdateCallBack updateCallback) {
        this.updateCallback = updateCallback;
    }

    public void setData(List<String> list){
        this.mUrlList.clear();
        mUrlList.addAll(list);
        this.notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return mUrlList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if(convertView==null){
            convertView = View.inflate(mContext, R.layout.item_downloadeddetail,null);
            holder = new Holder();
            holder.mTvchapterName = (TextView) convertView.findViewById(R.id.item_downloadeddetail_chapterName);
            holder.mTvcwName = (TextView) convertView.findViewById(R.id.item_downloadeddetail_cwName);
            holder.mPb = (ProgressBar) convertView.findViewById(R.id.item_downloadeddetail_pb);
            holder.mTvpb = (TextView) convertView.findViewById(R.id.item_downloadeddetail_tvpb);
            holder.mTvstatus1 = (TextView) convertView.findViewById(R.id.item_downloadeddetail_status1);
            holder.mTvstatus2 = (TextView) convertView.findViewById(R.id.item_downloadeddetail_status2);
            holder.mCheckBox = (ImageView) convertView.findViewById(R.id.item_downloadeddetail_check);
            convertView.setTag(holder);
        }else{
            holder = (Holder) convertView.getTag();
        }
        String url = mUrlList.get(position);
        if(updateCallback!=null){
            updateCallback.startProgress(url,position);
        }
        if(mDownloadManager !=null){
            String fileName = mDownloadManager.getFileName(url);
            holder.mTvcwName.setText(fileName);

            int progress = mDownloadManager.getProgress(url);
            holder.mPb.setProgress(progress);

            String fileDownloadedSize =FileUtil.FormetFileSize(mDownloadManager.getFileDownloadedSize(url));
            String fileSize = FileUtil.FormetFileSize(mDownloadManager.getFileSize(url));
            holder.mTvpb.setText(fileDownloadedSize+"/"+fileSize);

            DownloadStatus status = mDownloadManager.getStatus(url);
            switch (status){
                case DOWNLOADING:
                    holder.mTvstatus1.setText("下载中");
                    break;
                case STOP:
                    holder.mTvstatus1.setText("暂停中");
                    break;
                case WAITTING:
                    holder.mTvstatus1.setText("等待中");
                    break;
                case FAIL:
                    holder.mTvstatus1.setText("下载失败");
                    break;
                case SUCCESS:
                    holder.mTvstatus1.setText("下载成功");
                    break;
            }
        }


        return convertView;
    }


    class Holder{
        TextView mTvchapterName;
        TextView mTvcwName;
        ProgressBar mPb;
        TextView mTvpb;
        TextView mTvstatus1;
        TextView mTvstatus2;
        ImageView mCheckBox;
    }
}
